package doantotnghiep.daoimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.ImportedMedicamentDao;
import doantotnghiep.model.ImportedDetail;
import doantotnghiep.model.ImportedMedicament;

public class ImportedMedicamentDaoImpl implements ImportedMedicamentDao{

	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ImportedMedicament> listImportedMedicament() {
		return hibernateTemplate.find("from ImportedMedicament order by id");
	}

	@Override
	public void deleteImportedMedicament(int id) {
		ImportedMedicament entity = this.findImportedByID(id);
			if (entity != null)
				this.hibernateTemplate.delete(entity);
	}
		
	@SuppressWarnings("unchecked")
	private ImportedMedicament findImportedByID(int id) {
		List<ImportedMedicament> importedMedicaments = hibernateTemplate.find("from ImportedMedicament where id ="+ id);
		if(importedMedicaments!=null){
			return importedMedicaments.get(0);
		}
		return null;
	}

	@Override
	public void addImportedMedicament(ImportedMedicament importedMedicament) {
		 hibernateTemplate.saveOrUpdate(importedMedicament);
	}

	@Override
	public void addDetailImported(ImportedDetail details) {
		hibernateTemplate.save(details);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public ImportedMedicament getInsertedImportedMedicament() {
		List<ImportedMedicament> resultSet = hibernateTemplate.findByNamedQuery("getImportedID");
		if(resultSet!=null){
			ImportedMedicament importedMedicament =(ImportedMedicament) resultSet.get(0);
			return importedMedicament;	
		}
			return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ImportedDetail> getDetailByImportedID(int id) {
		List<ImportedDetail> details = hibernateTemplate.find("from ImportedDetail where MaPhieuNhap = "+id);
		return details;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ImportedMedicament> listImportedMedicamentByDate(String date) {
		List<ImportedMedicament> details =  hibernateTemplate.find("from ImportedMedicament where importedDate = '"+date+"'"+" order by id");
		return details;
	}

}
